package com.pearl.db;

import com.pearl.entitys.dataBaseTable.Role;
import com.pearl.utils.DateUtils;
import com.pearl.utils.Where;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDao {

  private static final String SQL_INS = "INSERT INTO role(id,role_id,name,description,status,create_time,update_time) VALUES (?,?,?,?,?,?,?)";

  private static final String SQL_UPD = "UPDATE role SET role_id=?,name=?,description=?,status=?,create_time=?,update_time=? WHERE id=?";

  private static final String SQL_SEL = "SELECT id,role_id,name,description,status,create_time,update_time FROM role ";

  private final Connection conn;

  public RoleDao(Connection conn) {
    this.conn = conn;
  }

  public int insert(Role data) {
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_INS)) {
      ps.setString(1, data.getId());
      ps.setString(2, data.getRoleId());
      ps.setString(3, data.getName());
      ps.setString(4, data.getDescription());
      ps.setInt(5, data.getStatus());
      DateUtils.setDateTz(ps, 6, data.getCreateTime());
      DateUtils.setDateTz(ps, 7, data.getUpdateTime());

      return ps.executeUpdate();
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public int insert(List<Role> dataList) {
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_INS)) {
      for (Role data : dataList) {
        ps.setString(1, data.getId());
        ps.setString(2, data.getRoleId());
        ps.setString(3, data.getName());
        ps.setString(4, data.getDescription());
        ps.setInt(5, data.getStatus());
        DateUtils.setDateTz(ps, 6, data.getCreateTime());
        DateUtils.setDateTz(ps, 7, data.getUpdateTime());

        ps.addBatch();
      }
      return ps.executeBatch().length;
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public int update(Role data) {
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_UPD)) {
      ps.setString(1, data.getRoleId());
      ps.setString(2, data.getName());
      ps.setString(3, data.getDescription());
      ps.setInt(4, data.getStatus());
      DateUtils.setDateTz(ps, 5, data.getCreateTime());
      DateUtils.setDateTz(ps, 6, data.getUpdateTime());
      ps.setString(7, data.getId());

      return ps.executeUpdate();
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public int update(List<Role> dataList) {
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_UPD)) {
      for (Role data : dataList) {
        ps.setString(1, data.getRoleId());
        ps.setString(2, data.getName());
        ps.setString(3, data.getDescription());
        ps.setInt(4, data.getStatus());
        DateUtils.setDateTz(ps, 5, data.getCreateTime());
        DateUtils.setDateTz(ps, 6, data.getUpdateTime());
        ps.setString(7, data.getId());

        ps.addBatch();
      }
      return ps.executeBatch().length;
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public int delete(String id) {
    try (PreparedStatement ps = this.conn.prepareStatement("DELETE FROM role WHERE id=?")) {
      ps.setString(1, id);

      return ps.executeUpdate();
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public List<Role> selectAll() {
    ArrayList<Role> result = new ArrayList<Role>();
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_SEL)) {

      ResultSet rs = ps.executeQuery();
      while (rs.next()) {
        result.add(convert(rs));
      }
      return result;
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }


  public Role selectByPK(String id) {
    Role result = null;
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_SEL + "WHERE id=?")) {
      ps.setString(1, id);

      ResultSet rs = ps.executeQuery();
      if (rs.next()) {
        result = convert(rs);
      }
      return result;
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public List<Role> selectWhere(Where where) {
    List<Role> result = new ArrayList<>();
    try (PreparedStatement ps = where.prepareStatement(conn, SQL_SEL)) {
      ResultSet rs = ps.executeQuery();
      while (rs.next()) {
        result.add(convert(rs));
      }
      return result;
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  private Role convert(ResultSet rs) throws SQLException {
    Role data = new Role();
    int index = 1;
    data.setId(rs.getString(index++));
    data.setRoleId(rs.getString(index++));
    data.setName(rs.getString(index++));
    data.setDescription(rs.getString(index++));
    data.setStatus(rs.getInt(index++));
    data.setCreateTime(DateUtils.getDateTz(rs, index++));
    data.setUpdateTime(DateUtils.getDateTz(rs, index++));

    return data;
  }
}
